package com.zlc.platform.controller;

import com.zlc.common.common.entity.PageVo;
import com.zlc.common.core.sys.model.vo.MayanSysNotificationVo;
import com.zlc.common.model.sys.NotificationRequest;
import com.zlc.common.util.UserHelper;
import com.zlc.platform.service.NotificationService;
import com.zlc.platform.service.SysNotificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统通知控制器
 */

@Slf4j
@Api(tags = "文件操作相关")
@RestController
@RequestMapping("/api/notification")
public class NotificationController {

    @Autowired
    private SysNotificationService sysNotificationService;
    @Autowired
    private NotificationService notificationService;

    @GetMapping(value = "/findNoShowSse", produces = "text/event-stream;charset=UTF-8")
    public Flux<String> findNoShowSse(HttpServletResponse response) {
        return sysNotificationService.findNoShowSse();
    }

    @PostMapping(value = "/remove")
    public void remove(@RequestBody List<String> ids) {
        sysNotificationService.delete(ids);
    }

    @ApiOperation(value = "查询当前用户通知列表 最新20条")
    @GetMapping(value = "/findList")
    public List<MayanSysNotificationVo> findList() {
        return sysNotificationService.findList();
    }

    @ApiOperation(value = "分页查询当前用户通知列表")
    @PostMapping(value = "/findPage")
    public PageVo<MayanSysNotificationVo> findPage(@RequestBody NotificationRequest request) {
        return sysNotificationService.findPage(request);
    }

    @ApiOperation(value = "查询所有未读数量")
    @PostMapping(value = "/findNoReadCount")
    public Integer findNoReadCount() {
        return sysNotificationService.findNoReadCount();
    }

    @ApiOperation(value = "查询所有未读数量")
    @GetMapping(value = "/findNoReadCountSSE",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> findNoReadCountSSE() {
        return sysNotificationService.findNoReadCountSse();
    }


    @ApiOperation(value = "已读多个")
    @PostMapping(value = "/read")
    public void read(@RequestBody List<String> ids) {
        sysNotificationService.read(ids);
    }

    @ApiOperation(value = "已读全部")
    @PostMapping(value = "/readAll")
    public void readAll() {
        sysNotificationService.readAll();
    }

    @ApiOperation(value = "测试发送消息")
    @PostMapping(value = "/sendTest")
    public void sendTest(@RequestBody MayanSysNotificationVo notificationVo) {
        notificationService.sendNotification(UserHelper.getNowAccount(), notificationVo.getTitle(), notificationVo.getContent());
    }
}
